<?php
/*
write by vomtung@gmail.com
use to edit User
*/
 class Mobileservice_EdituserController extends Core_Controller_Action_Standard
{
//not Finish
  public function indexAction()
  {
  $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	
	
	$username=$_POST['username'];
	$password=$_POST['password'];
	$passwordagain=$_POST['passwordagain'];
	$user = Engine_Api::_()->getItem('user', $userid);
	//echo 'server'.$user->level_id;
	
	//return;
	$userLevel = Engine_Api::_()->getItem('authorization_level', (int)'1');
	$superAdminLevels = Engine_Api::_()->getItemTable('authorization_level')->fetchAll(array(
      'flag = ?' => 'superadmin',
    ));
	
	 // Get networks
    $select = Engine_Api::_()->getDbtable('membership', 'network')->getMembershipsOfSelect($user);
    $networks = Engine_Api::_()->getDbtable('networks', 'network')->fetchAll($select);
    $values['network_id'] = $oldNetworks = array();
    foreach( $networks as $network ) {
      $values['network_id'][] = $oldNetworks[] = $network->getIdentity();
    }
	
	$values=array(
	'email'=>$emailaddress,
	'username'=>$username,
	'password'=>$password,
	'password_conf'=>$passwordagain
	);
	echo "aaa";

  }
  public function editemailAction()
  {
	$this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	$userid=$_POST['userid'];
	$emailaddress = $_POST['emailaddress'];
	//$userid='1';
	//$emailaddress = 'vomtung@gmail.com';
	$values=array(
	'email'=>$emailaddress
	);
	$user = Engine_Api::_()->getItem('user', $userid);
	$user->email=$emailaddress;
    $user->save();
			$jsondata = '{  "result":"'.'successfull'.'",
		 }';
		echo json_encode(array("result"=>"Edit Email Success"));
  }
  public function editusernameAction()
  {
	$this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	$userid=$_POST['userid'];
	$username = $_POST['username'];
	//$userid='1';
	//$emailaddress = 'vomtung@gmail.com';
	$values=array(
	'username'=>$username
	);
	$user = Engine_Api::_()->getItem('user', $userid);
	$user->username=$username;
    $user->save();
			$jsondata = '{  "result":"'.'successfull'.'",
		 }';
		echo json_encode(array("result"=>"Edit UserName Success"));
  }
  public function editdisplaynameAction()
  {
	$this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	$userid=$_POST['userid'];
	$displayname = $_POST['displayname'];
	//$userid='1';
	//$emailaddress = 'vomtung@gmail.com';
	$values=array(
	'displayname'=>$displayname
	);
	$user = Engine_Api::_()->getItem('user', $userid);
	$user->displayname=$displayname;
    $user->save();
			$jsondata = '{  "result":"'.'successfull'.'",
		 }';
		echo json_encode(array("result"=>"Edit Display Name Success"));
  }
  public function getuserinfofromidAction()
  {
	 $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	$userid=$_POST["userid"];
	
	//$announcement_id='2';
	$user = Engine_Api::_()->getItem('user', $userid);
   echo json_encode(array(
   "username"=>$user->username,
   "useremail"=>$user->email,
   "displayname"=>$user->displayname
   ));
  }
  public function edituserpasswordAction()
  {
	 $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	$userid=$_POST['userid'];
	$password = $_POST['password'];
	$user = Engine_Api::_()->getItem('user', $userid);
	$user->password=$password;
    $user->save();
	echo json_encode(array("result"=>"Change User Password Success"));
  }
   public function showalllevelAction()
  {
  $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	//start
	
	 // Get select
   $table = Engine_Api::_()->getDbtable('levels', 'authorization');
    $select = $table->select();
	$paginator = Zend_Paginator::factory($select);
	 $jsondata = '{  "Name":"Handoiviem",'.' "Data":[';
	 foreach( $paginator as $item ) {
      if( !empty($item->level_id ) ) {
        //$identities[] =array(array( $item->user_id , $item->username));
		$jsondata = $jsondata .'{ "level_id":"'.$item->level_id .
		'","title":"'.$item->title.
		'","description":"'.$item->description.
		'" },';
      }
    }
	$jsondata=$jsondata.' ]}';
	//end
//	$identities =array('fruit' => 'apple');
//	$identities['veggie']= 'carrot';
//	$name=$identities['veggie'];
	//$name='1';
	//$name='handoiviem';
//echo $jsondata;
echo $_GET["callback"].'('.$jsondata.')';
  }
   public function confirmedituserlevelAction()
  {
	$this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	//start
	$userid=$_POST['userid'];
	$level_id = $_POST['level_id'];
	$user = Engine_Api::_()->getItem('user', $userid);
	$user->level_id	=$level_id	;
    $user->save();
	echo json_encode(array("result"=>"Change User Level Success"));
  }
   public function approveverifiedenableAction()
  {
	$this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	//start
	$userid=$_POST['userid'];
	$approved = $_POST['userapproved'];
	$verified = $_POST['userverified'];
	$enabled = $_POST['userenabled'];
	$user = Engine_Api::_()->getItem('user', $userid);
	$user->approved	=$approved;
	$user->verified	=$verified;
	$user->enabled	=$enabled;
    $user->save();
	echo json_encode(array("result"=>'Change '.$user->approved.'Setting '.$user->verified	.'User '.$user->enabled.' Success '.$enabled));
  }
  public function deleteuserAction()
  {
	$this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	//start
	$id = $_POST['userid'];
    $user = Engine_Api::_()->getItem('user', $id);
    // deleting user
    //$form->user_id->setValue($id);
      $db = Engine_Api::_()->getDbtable('users', 'user')->getAdapter();
      $db->beginTransaction();

      try {
        $user->delete();
        
        $db->commit();
      } catch( Exception $e ) {
        $db->rollBack();
        throw $e;
      }
	echo json_encode(array("result"=>"Delete User Success"));
  }
  public function userstatsAction()
  {
	 $this->_helper->layout->disableLayout();
   $this->_helper->viewRenderer->setNoRender();
	//start
    // Get types
	$id =  $_POST['userid'];
    //$id = '4';
    $this->view->user = $user = Engine_Api::_()->getItem('user', $id);

    $fieldsByAlias = Engine_Api::_()->fields()->getFieldsObjectsByAlias($user);

    if( !empty($fieldsByAlias['profile_type']) ) {
      $optionId = $fieldsByAlias['profile_type']->getValue($user);
      if( $optionId ) {
        $optionObj = Engine_Api::_()->fields()
          ->getFieldsOptions($user)
          ->getRowMatching('option_id', $optionId->value);
        if( $optionObj ) {
          $this->view->memberType = $optionObj->label;
        }
      }
    }

    // Networks
    $select = Engine_Api::_()->getDbtable('membership', 'network')->getMembershipsOfSelect($user)
      ->where('hide = ?', 0);
    $this->view->networks = Engine_Api::_()->getDbtable('networks', 'network')->fetchAll($select);

    // Friend count
    $this->view->friendCount = $user->membership()->getMemberCount($user);
	//end
//	$identities =array('fruit' => 'apple');
//	$identities['veggie']= 'carrot';
//	$name=$identities['veggie'];
	//$name='1';
	//$name='handoiviem';
//echo $jsondata;
	$profileview=$user->view_count;
	$friends=$user->member_count;
	$lastupdate=$user->modified_date;
	$joined=$user->creation_date;
	$ipObj = new Engine_IP($user->creation_ip);
	$userip= $ipObj->toString();
	$ipObjLast= new Engine_IP($user->creation_ip);
	$userlastloginip=$ipObjLast->toString();
	echo json_encode(array(
	"profileview"=>$profileview,
	"friends"=>$friends,
	"lastupdate"=>$lastupdate,
	"joined"=>$joined,
	"userip"=>$userip,
	"lastlogin_date"=>$user->lastlogin_date,
	"lastlogin_ip"=>$userlastloginip
	));
  }
}
 ?>